package com.example.allshengmedical.experimentrun.activity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import com.example.allshengmedical.util.FileManagerUtil;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author boye009 扩增数据库
 * 
 *         暂没用到
 */
public class AmplifucationDBHelper {
	final static String AmplifucationTableName = "AmplifucationDB";
	final static String ChannelIndex = "ChannelIndex";
	final static String LightNum = "LightNum";
	final static String Line = "Line";
	final static String PointIndex = "PointIndex";
	public static final int VERSION = 1;

	// public static final String TABLENUMBER = "1";
	SQLiteDatabase database;
	File file;
	Context context;

	public AmplifucationDBHelper(Context ctx) {
		// TODO Auto-generated constructor stub
		File file = createDBFile();
		this.context = ctx;
		this.database = SQLiteDatabase.openOrCreateDatabase(file, null);
	}

	/**
	 * @param channelIndex
	 *            通道索引
	 * @param lightNum
	 *            光强值
	 * @param line
	 *            第几条
	 * @param pointIndex
	 *            第几次
	 */
	public void add(String channelIndex, String lightNum, String line,
			String pointIndex) {

		ContentValues contentValues = new ContentValues();
		contentValues.put(ChannelIndex, Integer.parseInt(channelIndex));
		contentValues.put(LightNum, Float.parseFloat(lightNum));
		contentValues.put(Line, Integer.parseInt(line));
		contentValues.put(PointIndex, Integer.parseInt(pointIndex));
		database.insert(AmplifucationTableName, null, contentValues);
		contentValues.clear();
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void close() {
		if (database != null)
			database.close();
	}

	public File createDBFile() {
		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		fileManagerUtil.createDB();
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + CommonSQLiteHelper.DATABASE_NAME);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;

	}

	public ArrayList<String> queryChannel() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT ChannelIndex FROM "
				+ AmplifucationTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor
					.getColumnIndex("ChannelIndex")) + "";
			arrayList.add(string);
		}
		return arrayList;

	}

	public ArrayList<String> queryWhereChannel(int t) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where ChannelIndex = " + t, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum"))
					+ "";
			arrayList.add(string);
		}
		return arrayList;

	}

	public ArrayList<String> queryLight() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum"))
					+ "";
			arrayList.add(string);
		}
		return arrayList;

	}

	public ArrayList<String> queryLine(int line) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where Line = " + line, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum"))
					+ "";

			arrayList.add(string);
		}
		return arrayList;

	}

	/**
	 * 查询第几条线第几通道
	 * 
	 * @param db
	 * @param line
	 * @param channelIndex
	 * @return
	 */
	public ArrayList<Float> queryLine(int line, int channelIndex) {
		ArrayList<Float> arrayList = new ArrayList<Float>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where Line = " + (line)
				+ " and ChannelIndex=" + channelIndex, null);
		while (cursor.moveToNext()) {
			arrayList.add(cursor.getFloat(cursor.getColumnIndex("LightNum")));
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return arrayList;

	}

	public synchronized ArrayList<String> queryLineAmplicMethod(int line, int channelIndex) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where ChannelIndex = " + (channelIndex)
				+ " and Line=" + line, null);
		while (cursor.moveToNext()) {
			arrayList.add(cursor.getFloat(cursor.getColumnIndex("LightNum"))
					+ "");
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return arrayList;

	}

	/**
	 * 查询第几条线第几通道第几个点
	 * 
	 * @param db
	 * @param line
	 * @param channelIndex
	 * @param PointIndex
	 * @return
	 */
	public Float queryLine(int line, int channelIndex, int PointIndex) {
		Float f = null;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where Line = " + (line)
				+ " and ChannelIndex=" + channelIndex + " and PointIndex="
				+ PointIndex, null);
		while (cursor.moveToNext()) {
			f = cursor.getFloat(cursor.getColumnIndex("LightNum")) ;
		}
		// tempArrayList = dbHelper.query();
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}
		cursor.close();
		return f;

	}
	public Float queryLine(int line, int channelIndex, int PointIndex,
			int channelsize, boolean haveChannel1, boolean haveChannel2,
			boolean haveChannel3, boolean haveChannel4) {
		Float LightNum = 0f;
		int id = 0;
		id = calculateID(line, channelIndex, PointIndex, channelsize,
				haveChannel1, haveChannel2, haveChannel3, haveChannel4);
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where _id = " + (id), null);
		// Cursor cursor = database.rawQuery("SELECT LightNum FROM "
		// + RongJieTableName + " where Line = " + (line)
		// + " and ChannelIndex=" + channelIndex + " and PointIndex="
		// + PointIndex, null);
		if (cursor.moveToLast()) {
			LightNum = cursor.getFloat(cursor.getColumnIndex("LightNum"));
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		// db.close();
		cursor.close();
		return LightNum;

	}

	private int calculateID(int line, int channelIndex, int PointIndex,
			int channelsize, boolean haveChannel1, boolean haveChannel2,
			boolean haveChannel3, boolean haveChannel4) {
		int id = 0;
		if (channelsize == 4) {
			if (PointIndex == 1) {
				id = line + 1 + (channelIndex - 1) * 32;
			} else {
				id = (PointIndex - 1) * 32 * channelsize + (channelIndex - 1)
						* 32 + line + 1;
			}
		} else if (channelsize == 1) {
			id = line + 1 + (PointIndex - 1) * 32;
		} else if (channelsize == 2) {
			if (haveChannel1 && haveChannel2) {
				id = (PointIndex - 1) * 32 * channelsize + (channelIndex - 1)
						* 32 + line + 1;
			}
			if (haveChannel1 && haveChannel3) {
				if (channelIndex == 1) {
					id = (PointIndex - 1) * 32 * channelsize
							+ (channelIndex - 1) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32
							+ line + 1;
				}

			}
			if (haveChannel1 && haveChannel4) {
				if (channelIndex == 1) {
					id = (PointIndex - 1) * 32 * channelsize + (0) * 32 + line
							+ 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (1) * 32 + line
							+ 1;
				}
			}
			if (haveChannel2 && haveChannel3) {
				if (channelIndex == 2) {
					id = (PointIndex - 1) * 32 * channelsize + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32
							+ line + 1;
				}
			}
			if (haveChannel2 && haveChannel4) {
				if (channelIndex == 2) {
					id = (PointIndex - 1) * 32 * channelsize + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32
							+ line + 1;
				}
			}
			if (haveChannel3 && haveChannel4) {
				if (channelIndex == 3) {
					id = (PointIndex - 1) * 32 * channelsize + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32
							+ line + 1;
				}
			}
		} else if (channelsize == 3) {
			if (haveChannel1 && haveChannel2 && haveChannel3) {
				id = (PointIndex - 1) * 32 * 3 + (channelIndex - 1) * 32 + line
						+ 1;
			}
			if (haveChannel1 && haveChannel2 && haveChannel4) {
				if (channelIndex == 4) {
					id = (PointIndex - 1) * 32 * 3 + (3) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * 3 + (channelIndex - 1) * 32
							+ line + 1;
				}
			}
			if (haveChannel1 && haveChannel4 && haveChannel3) {
				if (channelIndex == 4) {
					id = (PointIndex - 1) * 32 * 3 + (2) * 32 + line + 1;
				} else if (channelIndex == 3) {
					id = (PointIndex - 1) * 32 * 3 + (1) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * 3 + (0) * 32 + line + 1;
				}
			}
			if (haveChannel4 && haveChannel2 && haveChannel3) {
				if (channelIndex == 2) {
					id = (PointIndex - 1) * 32 * 3 + (0) * 32 + line + 1;
				} else if (channelIndex == 3) {
					id = (PointIndex - 1) * 32 * 3 + (1) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * 3 + (2) * 32 + line + 1;
				}
			}
		}
		return id;
	}
	public ArrayList<String> queryLine() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT Line FROM "
				+ AmplifucationTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("Line")) + "";
			arrayList.add(string);
		}
		cursor.close();
		return arrayList;

	}

	public int queryPointIndex() {
		int i = 0;
		Cursor cursor = database.rawQuery("SELECT PointIndex FROM "
				+ AmplifucationTableName, null);
		if (cursor.moveToLast()) {
			String string = cursor.getInt(cursor.getColumnIndex("PointIndex"))
					+ "";
			if (!TextUtils.isEmpty(string)) {
				i = Integer.parseInt(string);
			}
		}
		return i;

	}

	public int queryID(int line, int channelIndex, int PointIndex) {
		int arrayList = 0;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where Line = " + (line)
				+ " and ChannelIndex=" + channelIndex + " and PointIndex="
				+ PointIndex, null);
		while (cursor.moveToNext()) {
			arrayList = cursor.getInt(cursor.getColumnIndex("_id"));
			SystemClock.sleep(1);
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return arrayList;

	}

	public String queryChannelByID(int id) {
		String arrayList = null;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where _id = " + (id), null);
		while (cursor.moveToNext()) {
			arrayList = cursor.getInt(cursor.getColumnIndex("ChannelIndex"))
					+ "";
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return arrayList;

	}

	public String queryLineByID(int id) {
		String arrayList = null;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where _id = " + (id), null);
		if (cursor.moveToLast()) {
			arrayList = cursor.getInt(cursor.getColumnIndex("Line")) + "";
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return arrayList;

	}

	public String queryPointIndexByID(int id) {
		String arrayList = null;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where _id = " + (id), null);
		while (cursor.moveToNext()) {
			arrayList = cursor.getInt(cursor.getColumnIndex("PointIndex")) + "";
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return arrayList;

	}
	public boolean queryWhereChannelIsHave(int t) {
		boolean ishave = false;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM "
				+ AmplifucationTableName + " where ChannelIndex = " + t, null);
		cursor.moveToFirst();
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum"))
					+ "";
			if (string != null || "".equals(string)) {
				ishave = true;
				break;
			}
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return ishave;

	}

	// @Override
	// public void onUpgrade( , int oldVersion, int newVersion)
	// {
	// // TODO Auto-generated method stub
	//
	// }

}
